/**
 * @file holding/Home.js
 *  实例持仓
 * @author
 */

 import React, {Component, PropTypes} from 'react';
 import {connect} from 'dva';
 import {routerRedux, Link, withRouter} from 'dva/router';
 import {
   Button,
   Modal,
   Row,
   Col,
   Table,
   Icon,
   Card,
   Form,
   message
 } from 'antd';
 import moment from 'moment';
 import Tabtitle from '../../../components/common/TabTitle';
 import Tablelist from '../../../components/common/TableData';
 // import DateRangePicker from '../component/searchBar/Datesearch';
 import DateRangePicker from '../component/searchBar/DateSearchBar';
 import styles from './home.less';


 const nowDate = moment(new Date()).format('YYYYMMDD');
 const params = {
   table: 'ifholding',
   pagesize: 15,
   currentpage: 1
 };
 const Holding = ({
   dispatch,
   holding,
   location: {
     pathname,
     query
   }
 }) => {
   if (holding.status) {
     const {holdinglist,currentpage, page, state, begindate, enddate,msg} = holding;
     console.log('holding>>>>', holding,);
     const tag = "holding";
     const columns = [
       {
         title: '交易日期',
         dataIndex: 'tradeDay',
         key: 'tradeDay',
         sorter: true,
       },
       {
         title: '基金号',
         dataIndex: 'fundId',
         key: 'fundId',
       },
       {
         title: '组合号',
         dataIndex: 'combiNo',
         key: 'combiNo',
       },
       {
         title: '组合名称',
         dataIndex: 'combiName',
         key: 'combiName',
       },
       {
         title: '实例号',
         dataIndex: 'instanceId',
         key: 'instanceId',
         sorter: true,
       },
       {
         title: '证券名称',
         dataIndex: 'stockCode',
         key: 'stockCode',
         sorter: true,
       },
       {
         title: '多空',
         dataIndex: 'contractType',
         key: 'contractType',
       },
       {
         title: '持仓数',
         dataIndex: 'endHoldingAmount',
         key: 'endHoldingAmount',
         sorter: true,
       },
       {
         title: '开仓总数',
         dataIndex: 'totalOpenAmount',
         key: 'totalOpenAmount',
       },
       {
         title: '开仓基差',
         dataIndex: 'openBasisSpread',
         key: 'openBasisSpread',
       },
       {
         title: '平仓总数',
         dataIndex: 'totalCloseAmount',
         key: 'totalCloseAmount',
       },
       {
         title: '平仓基差',
         dataIndex: 'closeBasisSpread',
         key: 'closeBasisSpread',
       },
       {
         title: '收盘基差',
         dataIndex: 'eodBasisSpread',
         key: 'eodBasisSpread',
       },
       {
         title: '基差盈亏',
         dataIndex: 'basisSpreadProfit',
         key: 'basisSpreadProfit',
       },
     ];
     // 分页
     function onShowSizeChange(current, pagesize) {
       dispatch({
         type: 'holding/changeRouter',
         payload: {
           table: 'ifholding',
           pagesize,
           currentpage:1,
           begindate: begindate,
           enddate: enddate,
           sortField: query.sortField,
            sortOrder: query.sortOrder,
         }
       })
     }
     function onChangecurrentpage(current,pagesize) {
       dispatch({
         type: 'holding/changeRouter',
         payload: {
           table: 'ifholding',
           currentpage: current,
           pagesize:pagesize,
           begindate: begindate,
           enddate: enddate,
           sortField: query.sortField,
           sortOrder: query.sortOrder,
         }
       })
     }
     //排序
     function onSorterChange(pagesize, filters, sorter) {
       const orderField = sorter.field;
       const orderSort = sorter.order;
       console.log(sorter, "orderField>>", orderField, "orderSort", orderSort);
       if(orderField !== undefined && orderSort !== undefined){
          if(query.pagesize&&query.begindate&&query.enddate){
            dispatch({
               type: 'holding/changeRouter',
               payload: {
                 table: 'ifholding',
                 currentpage: currentpage,
                 pagesize:query.pagesize,
                 sortField: orderField,
                 sortOrder: orderSort,
                 begindate: begindate,
                 enddate: enddate
               }
             })
          }else{
            dispatch({
              type: 'holding/changeRouter',
              payload: {
                 table: 'ifholding',
                 currentpage: currentpage,
                 pagesize:15,
                 begindate,
                 enddate,
                 sortField: orderField,
                 sortOrder: orderSort,
               }
             })
         }
       }else{
        return null
       }
       
    }
    //导出
    function downLoad(){
      const nowBegindate = begindate || moment(new Date()).format('YYYYMMDD');
      const nowEnddate = enddate || moment(new Date()).format('YYYYMMDD');
      window.location.href=location.href.indexOf('localhost')>-1?`/api/ifdata/history/export?table=ifholding&begindate=${nowBegindate}&enddate=${nowEnddate}`:`/thfx/api/ifdata/history/export?table=ifholding&begindate=${nowBegindate}&enddate=${nowEnddate}`;
    }
    function queryData() {
    console.log('beginDate>>>',begindate,'endDate>>>',enddate)
    if(begindate == "" || enddate == "" || begindate == "Invalid date"){
      message.warning('请选择时间');
      return;
    }else{
      dispatch({
        type: `${tag}/changeRouter`,
        payload: {
          table:`if${tag}`,
          pagesize:15,
          currentpage:1,
          begindate:begindate || BD,
          enddate:enddate || ED
        }
      })
      dispatch({
        type: `${tag}/setState`,
        payload: {
          'isRefresh':0
        }
      })
    }
  }
     return (
       <div>
         <Tabtitle value={'实例持仓'}/>
         <div className="page-holding-home content-inner">
           <Row className={styles.toolbar} type="flex" justify="start" align="middle">
             <Col span={4}>
               <DateRangePicker tag={tag} dispatch={dispatch} begindate={begindate} enddate={enddate} location={location}/>
             </Col>
             <Col span={18} push={2}>
              <Button type="primary" onClick={queryData}>
                查询
              </Button>
            </Col>
             <Col span={2}>
               <Button type="primary" onClick={downLoad}>导出</Button>
             </Col>
           </Row>
           
             <Tablelist columns={columns} data={holdinglist} page={page} onShowSizeChange={onShowSizeChange} onChangecurrentpage={onChangecurrentpage} onSorterChange={onSorterChange} record={record => `${record.instanceId}-${record.fundId}-${record.combiNo}-${record.stockCode}-${record.contractType}-${record.endHoldingAmount}-${record.tradeDay}-${record.basisSpreadProfit}-${record.eodBasisSpread}-${record.totalCloseAmount}-${record.combiName}-${record.totalOpenAmount}-${record.openBasisSpread}-${record.closeBasisSpread}`}/>
          
         </div>
       </div>
     )
   } else {
     return null
   }
 }

 Holding.propTypes = {}
 export default connect(({holding}) => ({holding}))(Holding)
